package com.tanhua.manager.controller;

import com.tanhua.domain.db.Admin;
import com.tanhua.manager.exceptions.TanhuaException;
import com.tanhua.manager.service.AdminService;
import com.tanhua.manager.util.AipFaceHelper;
import com.tanhua.manager.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录控制器
 */
@Controller

public class LoginController {
    @Autowired
    private AdminService adminService;

    /**
     * 检查登录
     * @return
     */
    @GetMapping("/login/checkLogin")
    @ResponseBody
    public ResponseEntity checkLogin() {
        return adminService.checkLogin();
    }
    /**
     * 微信回调
     * @param code
     * @return
     */
    @GetMapping("/login/callback")
    public void callback(@RequestParam String code) throws Exception{
        adminService.callback(code);
    }

    /**
     * 账号密码登录
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map param) {
        String username = (String) param.get("username");
        String password = (String) param.get("password");
        return adminService.login(username,password);
    }

    /**
     * 人脸登录
     */
    @PostMapping("/login/faceLogin")
    public String faceLogin(String images) {
        System.out.println("人脸登录接口方法");
        //调用工具类通过图片从百度查找我们自己的用户id
        String username = AipFaceHelper.faceSearch(images);
        System.out.println("username= "+username);
        //如果id存在，说明识别成功，需要查询数据库获取用户对象保存如session
        if(username != null) {
            Admin admin = adminService.queryAdminByUsername(username);
            if (admin != null) {
                return "redirect:/main.html";
            }else {
                return null;
            }

        }else{
            return null;
        }
    }
}
